package com.mhs.moreadapter;



import java.util.ArrayList;

import com.mhs.mhs_gui.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListView_Adapter extends BaseExpandableListAdapter 
{

	private Context context;
	private ArrayList<ExpandableListView_Category> groups; // an array list of categories
	private ArrayList<ArrayList<ExpandableListView_Item>> children; // an array list of items

	public ExpandableListView_Adapter(Context context)
	{
		this.context = context;
	}

	public ExpandableListView_Adapter(Context context, ArrayList<ExpandableListView_Category> groups,	ArrayList<ArrayList<ExpandableListView_Item>> children) 
	{
		this.context = context;
		this.groups = groups;
		this.children = children;
	}

	/**
	 * provide a method to add an item to a category dynamically, check if the
	 * category is existed otherwise throws an exception
	 *
	 * @param item
	 * @return void
	 */
	public void addItem(ExpandableListView_Item item) throws ClassNotFoundException 
	{
		// check if the category is existed
		if (!this.findCategory(groups, item.getCateId())) 
		{
			throw new ClassNotFoundException("Category doesn�t exist.");
		}
		else 
		{
			ExpandableListView_Category category = this.getCateInListById(groups, item.getCateId());
			int index = groups.indexOf(category);
			/*
			 * check if children list already contained the list to hold this
			 * group or else we have to create it
			 */
			if (children.size() < (index + 1)) {
				children.add(new ArrayList<ExpandableListView_Item>());
			}
			children.get(index).add(item);
		}
	}

	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}


	public long getChildId(int groupPosition, int childPosition) {
		ExpandableListView_Item item = (ExpandableListView_Item) children.get(groupPosition).get(childPosition);
		return item.getItemId();
	}


	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
	{
		ExpandableListView_Item item = (ExpandableListView_Item) getChild(groupPosition, childPosition);
		ExpandableListView_Category category = (ExpandableListView_Category) getGroup(groupPosition);

		if (convertView == null) 
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.expandablelistview_child_layout, null);
		}

		// set title for the item
		TextView tv = (TextView) convertView.findViewById(R.id.child_title);
		tv.setText(" " + item.getName());

		return convertView;
	}


	public int getChildrenCount(int groupPosition) {
		return children.get(groupPosition).size();
	}

	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}


	public int getGroupCount() {
		return groups.size();
	}


	public long getGroupId(int groupPostion) {
		return groups.get(groupPostion).getId();
	}


	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
	{
		ExpandableListView_Category group = (ExpandableListView_Category) getGroup(groupPosition);
		if (convertView == null) 
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.expandablelistview_group_layout, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.group_title);
		tv.setText(" " + group.getName());
		return convertView;
	}


	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	/**
	 * search for a category through out an array list rely on its id
	 *
	 * @param arraylist
	 * @param cateId
	 * @return true if found the category otherwise false
	 */
	private boolean findCategory(ArrayList<ExpandableListView_Category> arraylist, long cateId) 
	{
		for (ExpandableListView_Category category : arraylist) 
		{
			if (cateId == category.getId()) 
			{
				return true;
			}
		}
		return false;
	}
	/**
	 * method to get a category from a group depends on its id
	 * @param arraylist
	 * @param cateId
	 * @return category with the specific id if it exists otherwise return null
	 */

	private ExpandableListView_Category getCateInListById(ArrayList<ExpandableListView_Category> arraylist, long cateId)
	{
		for (ExpandableListView_Category category : arraylist) 
		{
			if (cateId == category.getId()) 
			{
				return category;
			}
		}
		return null;
	}
}